package com.ruipeng.shengyapo.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruipeng.shengyapo.comm.ResponseData;
import com.ruipeng.shengyapo.service.BikeService;
import com.ruipeng.shengyapo.service.CustomerService;
import com.ruipeng.shengyapo.utils.ContentUtils;
import com.ruipeng.shengyapo.utils.HttpUtils;
import com.ruipeng.shengyapo.utils.ObjUtils;

@Controller
@RequestMapping("/bike")
public class BikeController {

	@Autowired
	private BikeService bikeService;

	@Autowired
	private CustomerService customerService;

	/**
	 * 车辆故障
	 * @return
	 */
	@RequestMapping(value = "/{bikeId}/repair",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData addBikeError(@PathVariable String bikeId,int userId, String problemDesc , String problemPart,String lat,String lon){
		ResponseData data = new ResponseData();
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("bikeNo",bikeId);
        Map<String,Object> map = bikeService.getLockIMEIByBikeNo(params);
        if( ObjUtils.isEmpty(map)){
            data.setMsg("车辆不存在");
            data.setState(ContentUtils.COMM_STATE_FALSE);
        }else{
            params = new HashMap<>();
            params.put("bikeNo",map.get("bikeId"));
            params.put("userId",userId);
            params.put("problemDesc",problemDesc);
            params.put("problemPart",problemPart);
            params.put("lat",lat);
            params.put("lon",lon);
            customerService.addBikeError(params);
            data.setState(ContentUtils.COMM_STATE_TRUE);
        }
        return data;

	}

	/**
	 * 逾期退款未到账申诉信息
	 * @param orderNo
	 * @return
	 */
	@RequestMapping(value = "/{userId}/refundComplaint",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData refundComplaint(@PathVariable int userId, String orderNo,String rcImages,String rcType,String rcDesc){
		ResponseData data = new ResponseData();

		if( ObjUtils.isEmpty(orderNo) || ObjUtils.isEmpty(rcImages) || ObjUtils.isEmpty(rcType) || ObjUtils.isEmpty(userId)){
			System.out.print("orderNo="+orderNo+",orderImg="+rcImages+",type="+rcType);
			data.setMsg("参数不全");
			data.setState(ContentUtils.COMM_STATE_FALSE);
			return data;
		}else{

			Map<String,Object> params = new HashMap<String,Object>();
			params.put("orderNo",orderNo);
			params.put("orderImg",rcImages);
			params.put("type",rcType);
			params.put("userId",userId);
			params.put("userTel","");
			customerService.addRefundComplaint(params);
			data.setState(ContentUtils.COMM_STATE_TRUE);
			return data;
		}
	}
	/**
	 * 根据经纬度查询附近车辆
	 * @param lon
	 * @param lat
	 * @return
	 */
	@RequestMapping(value = "/getBikesAround",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getBikesAround( String lon, String lat) {

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("lon", lon);
		params.put("lat", lat);

		List<Map<String, Object>> bikeList = bikeService.getBikesAround(params);

		return HttpUtils.buildResponseBody4Map(ContentUtils.COMM_STATE_TRUE, bikeList);
	}
	/**
	 * 查询车辆详情
	 * @param bikeId
	 * @return
	 */
	@RequestMapping(value = "/info",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> bickInfo(String bikeId){
		Map<String,Object> map = new HashMap<String,Object>();
        map.put("bikeNo",bikeId);
		return bikeService.getLockIMEIByBikeNo(map);
	}
	/**
	 * 开锁
	 * @param userId
	 * @param bikeNo
	 * @return
	 */
	@RequestMapping(value = "/trip/{userId}/add",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData openLock(@PathVariable int userId, String bikeNo,String ridingPathStart) {
		ResponseData data = new ResponseData();

		if(ObjUtils.isEmpty(userId) || ObjUtils.isEmpty(bikeNo)){
			System.out.println("userId="+userId+",bikeNo="+bikeNo);
			data.setMsg("参数不全");
			data.setState(ContentUtils.COMM_STATE_FALSE);
			return data;
		}else{
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("userId",userId);
			params.put("bikeNo",bikeNo);
			params.put("ridingPathStart",ridingPathStart);
			data = bikeService.openBike(params,data);
		}

		return data;

	}


	/**
	 * 车辆报修
	 * @param bikeId 车辆ID
	 * @param lon 车辆所在经度
	 * @param lat 车辆所在纬度
	 * @param problemPart 问题部件
	 * @param problemDesc  问题描述
	 * @return
	 */
	@RequestMapping(value = "/bikeRepair",method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> saveBikeRepair(String bikeId, String lon, String lat, String problemPart, String problemDesc) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("bikeId", bikeId);
		params.put("lon", lon);
		params.put("lat", lat);
		params.put("problemPart", problemPart);
		params.put("problemDesc", problemDesc);

		int result = bikeService.saveBikeRepair(params);

		if (result>0) {
			return HttpUtils.buildResponseBody4Map(ContentUtils.COMM_STATE_TRUE, "感谢您对昇雅铂的帮助！");
		}else{
			return HttpUtils.buildResponseBody4Map(ContentUtils.COMM_STATE_FALSE, "报修失败，请在稍后再次尝试！");
		}
	}


	/**
	 * 关锁回调
	 * @return
	 */
	@RequestMapping(value = "closeBike",method = RequestMethod.POST)
	@ResponseBody
	public ResponseData closeBick(String bikeNo){
//	    System.out.println("bikeNo="+bikeNo);
		ResponseData data = new ResponseData();
//		Map<String,Object> params = new HashMap<String,Object>();
//		params.put("bikeNo",bikeNo);
//		bikeService.updateCloseBikeByOrderNo(params);
//		data.setState(ContentUtils.COMM_STATE_TRUE);
        data.setState(ContentUtils.COMM_STATE_FALSE);
        data.setMsg("系统关闭手动结算功能");

		return data;
	}


}
